package week03.resume;

import com.alibaba.fastjson.JSON;
import sun.org.mozilla.javascript.internal.json.JsonParser;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by zhangxiaokun on 2018/2/6.
 * 1. 现在有一个简历类Resume ，包括 名字，介绍，年龄，还有一组电话号码（List<String>类型）。
 现在，这个简历需要支持clone方法（深拷贝），请实现这个类。
 */
public class Resume implements Cloneable, Serializable, MyCloneable<Resume>, MyCloneable2<Resume>{
    private static final long serialVersionUID = -8582444496641740009L;

    private String name;                      //名字
    private String introduction;             //介绍
    private Integer age;                      //年龄
    private List<String> iphoneNumberList;  //电话号

    public Resume(String name, String introduction, Integer age, List<String> iphoneNumberList) {
        this.name = name;
        this.introduction = introduction;
        this.age = age;
        this.iphoneNumberList = iphoneNumberList;
    }
    public Resume() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<String> getIphoneNumberList() {
        return iphoneNumberList;
    }

    public void setIphoneNumberList(List<String> iphoneNumberList) {
        this.iphoneNumberList = iphoneNumberList;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        //复制属性
        String name = this.name + "";
        String introduction = this.introduction + "";
        Integer age = new Integer(this.age.intValue());
        //复制iphoneNumberList 属性
        String[] iphoneNumberListSrc =  (String[])this.iphoneNumberList.toArray();
        String[] iphoneNumberListDesc = new String[iphoneNumberListSrc.length];
        System.arraycopy(iphoneNumberListSrc, 0, iphoneNumberListDesc, 0, iphoneNumberListSrc.length);
        List<String> iphoneNumberList = Arrays.asList(iphoneNumberListSrc);     //
//        Arrays.copyOf();
        //构建目标实例
        Resume resume = (Resume) super.clone();
        resume.setName(name);
        resume.setIntroduction(introduction);
        resume.setAge(age);
        resume.setIphoneNumberList(iphoneNumberList);
        return resume;
    }

    //用 系列化反系列化 实现深度复制, 无论原型对象层次多深，结构多复杂， 直接丢给
    @Override
    public Resume myClone() {
        Resume resume = null;  //(Resume) super.clone();
        try {
            //系列化写入 内存缓存
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(byteArrayOut);
            oos.writeObject(this);

            //从内存缓存 读取，反系列化成对象
            byteArrayOut.toByteArray();
            ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(byteArrayOut.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(byteArrayInput);
            resume = (Resume) ois.readObject();

        } catch (IOException  | ClassNotFoundException  e ) {
            e.printStackTrace();
        }
        return resume;
    }

    //通过转json，再解析转回目标对象
    @Override
    public Resume myClone2() {
        Resume resume = null;
        String json = JSON.toJSONString(this);
        resume = JSON.parseObject(json, Resume.class);

        return resume;
    }


    //  测试clone()
    public static void main(String[] args) {

    }


}
